#include "data_handle.h"
#include "ui_data_handle.h"
#include "data_analysis.h"
#include "sql_handle.h"
#include "login.h"

#include <QSqlDatabase>
#include <QMessageBox>
#include <QDir>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QString>
#include <QDebug>
#include <string>
#include <QRegularExpression>

data_handle::data_handle(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::data_handle)
{
    ui->setupUi(this);
    ui->item_comboBox->addItem("红");
    ui->item_comboBox->addItem("绿");
    ui->item_comboBox->addItem("蓝");
    ui->item_comboBox->addItem("白");
    ui->item_comboBox->addItem("黑");
    ui->item_comboBox->addItem("橙");
}

data_handle::~data_handle()
{
    delete ui;
}

void createSaleTable(const std::string& user_info_user) {
    sql_handle data;
    QSqlDatabase db = data.db;

    if (!db.isOpen()) {
        qDebug() << "Database connection is not open.";
        return;
    }

    QString user = QString::fromStdString(user_info_user);
    QString createSaleTableQuery;
    qDebug() << user_info_user;

    // 定义正则表达式
    QRegularExpression regex("^[a-zA-Z0-9_]+$");

    // 使用 QRegularExpression 的 match() 方法检查字符串是否匹配
    QRegularExpressionMatch match = regex.match(user);
    if (!user.isEmpty() && match.hasMatch()) {
        // 使用字符串格式化生成 SQL 查询
        createSaleTableQuery = QString(R"(
            CREATE TABLE IF NOT EXISTS %1 (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                username TEXT NOT NULL,
                sales INTEGER DEFAULT 0
            )
        )").arg(user);

        qDebug() << createSaleTableQuery;
    } else {
        qDebug() << "Invalid table name.";
        return;
    }

    QSqlQuery query(db);
    if (!query.exec(createSaleTableQuery)) {
        qDebug() << "create sale table fail:" << query.lastError().text();
        return;
    } else {
        qDebug() << "create sale table success";
    }
}

void data_handle::on_submit_Button_2_clicked()
{
    QString selectedText = ui->item_comboBox->currentText();
    QString sale_str = ui->lineEdit->text();
    QString selectedRadioButton;

    if (ui->radioButton_1->isChecked()) {
        selectedRadioButton = "RadioButton1";
    } else if (ui->radioButton_2->isChecked()) {
        selectedRadioButton = "RadioButton2";
    } else {
        selectedRadioButton = "None";
    }

    sql_handle data;
    QSqlDatabase db = data.db;

    if (!db.isOpen()) {
        qDebug() << "Database connection is not open.";
        return;
    }

    QString user = QString::fromStdString(user_info.user);
    qDebug() << user;

    QRegularExpression regex("^[a-zA-Z0-9_]+$");
    QRegularExpressionMatch match = regex.match(user);
    if (!user.isEmpty() && match.hasMatch()) {
        QString createSaleTableQuery = QString(R"(
            CREATE TABLE IF NOT EXISTS %1 (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                username TEXT NOT NULL,
                sales INTEGER DEFAULT 0
            )
        )").arg(user);

        qDebug() << createSaleTableQuery;

        { // 使用作用域来确保 QSqlQuery 对象被销毁
            QSqlQuery query(db);
            if (!query.exec(createSaleTableQuery)) {
                qDebug() << "create sale table fail:" << query.lastError().text();
                return;
            } else {
                qDebug() << "create sale table success";
            }
        }

        bool ok;
        int sales = sale_str.toInt(&ok);
        if (!ok) {
            qDebug() << "Invalid sales value:" << sale_str;
            return;
        }

        QString insertQuery = QString("INSERT INTO %1 (username, sales) VALUES (:username, :sales)").arg(user);
        { // 使用新的 QSqlQuery 对象
            QSqlQuery query(db);
            query.prepare(insertQuery);
            query.bindValue(":username", user);
            query.bindValue(":sales", sales);

            qDebug() << "Executing query:" << insertQuery;
            qDebug() << "Bound values: username=" << user << ", sales=" << sales;

            if (!query.exec()) {
                qDebug() << "Insert sale data fail:" << query.lastError().text();
            } else {
                qDebug() << "Data inserted successfully!";
                QMessageBox::information(this, "选择的值", "提交成功");
            }
        }
    } else {
        qDebug() << "Invalid table name.";
    }
}


void data_handle::on_analysis_Button_clicked()
{
    data_analysis *s = new data_analysis;
    s->show();
    this->close();
}

